package com.pfs.internal.timestamp.reports;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.pfs.internal.timestamp.entities.Leave;
import com.pfs.internal.timestamp.utils.ExcelUtil;
import com.pfs.internal.timestamp.utils.TimeStampUtil;

public class LeavesReadExcel extends PfsReadExcel<Leave> {
	protected Workbook wb;

	public LeavesReadExcel(Workbook wb) {
		this.wb = wb;
		setStartRow(1);
	}

	public List<Leave> readExcel() throws Exception {
		HSSFSheet sheet = (HSSFSheet) wb.getSheetAt(0);
		List<Leave> leaves = this.readSheet(sheet);
		return leaves;
	}

	@Override
	public void setStartRow(int startRow) {
		this.startRow = startRow;
	}

	@Override
	public List<Leave> readRow(HSSFRow row, int rn) throws Exception {
		//System.out.println("ROW: " + row.getRowNum());
		String username;
		String fullname;
		Date fromDate;
		Date toDate;
		String halfDay;
		String leaveType;
		String reason;
		List<Leave> leaves = new ArrayList<Leave>();
		try {
			username = String.valueOf(ExcelUtil.getStringCellValue(row, 1));
			fullname = String.valueOf(ExcelUtil.getStringCellValue(row, 0));
			fromDate = ExcelUtil.getDateCellValue(row, 2);
			toDate = ExcelUtil.getDateCellValue(row, 3);
			try {
				halfDay = String.valueOf(ExcelUtil.getNumericCellValue(row, 4));
			} catch (Exception e) {
				halfDay = String.valueOf(ExcelUtil.getStringCellValue(row, 4));
			}
			leaveType = String.valueOf(ExcelUtil.getStringCellValue(row, 5));
			try {
				reason = String.valueOf(ExcelUtil.getStringCellValue(row, 6));
			} catch (Exception e) {
				reason = String.valueOf(ExcelUtil.getNumericCellValue(row, 6));
			}
			long dateDiff = TimeStampUtil.getDateDiff(toDate, fromDate);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(fromDate);
			if (dateDiff > 0) {
				int oldDay = calendar.get(Calendar.DAY_OF_MONTH);
				for (int i = 0; i <= dateDiff; i++) {
					calendar.set(Calendar.DAY_OF_MONTH, oldDay + i);
					Leave leave = new Leave();
					leave.setUsername(username);
					leave.setFullname(fullname);
					leave.setLeaveType(leaveType);
					Date effDate = calendar.getTime();
					leave.setFromDate(effDate);
					leave.setToDate(effDate);
					leave.setHalfDay(false);
					leave.setReason(reason);
					leaves.add(leave);
				}
			} else {
				Leave leave = new Leave();
				leave.setUsername(username);
				leave.setFullname(fullname);
				Date effDate = calendar.getTime();
				leave.setFromDate(effDate);
				leave.setToDate(effDate);
				leave.setLeaveType(leaveType);
				leave.setReason(reason);
				if (halfDay != null && halfDay.equals("0.5")) {
					leave.setHalfDay(true);
				}
				leaves.add(leave);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return leaves;
	}

}
